﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;

namespace MipsPipelineSimulator
{
    public partial class RegisterControl : UserControl
    {
        public RichListView registerListView = null;

        private Register[] registers = new Register[32];

        public RegisterControl()
        {
            InitializeComponent();
        }

        private void RegisterControl_Load(object sender, EventArgs e)
        {
            registerListView = new RichListView(2, false);
            ColumnHeader name = new ColumnHeader();
            name.Text = "寄存器名称";
            name.Width = 123;
            ColumnHeader value = new ColumnHeader();
            value.Text = "寄存器值";
            value.Width = 123;
            registerListView.Columns.AddRange(new ColumnHeader[2] { name, value });
            registerListView.Dock = DockStyle.Fill;
            registerListView.Location = new Point(0, 0);
            this.Controls.Add(registerListView);

            registerListView.SetColumnEditable(2, new RichListView.validate(this.validateValue));

            for (int i = 1; i <= 32; i++)
            {
                Register register = new Register();
                register.typeCode = TypeCode.Int32;
                register.value = 0;
                register.name = "$" + i.ToString();
                registers[i - 1] = register;
                string[] s = new string[2];
                s[0] = register.name;
                s[1] = this.RegToStr(register);
                registerListView.Items.Add(new ListViewItem(s));
            }
        }

        public bool validateValue(string inputStr)
        {
            Regex r = new Regex(@"[0-9A-Fa-f]{1,8}$");
            return r.Match(inputStr).Success;
        }

        private string RegToStr(Register register)
        {
            string tmp = null;
            tmp = CommonTool.NumToStr(register.typeCode, register.value, "x", true);
            return tmp;
        }
    }
}
